package gbench.appdemo.crawler.junit.analyzer;

import org.junit.jupiter.api.Test;

import gbench.common.fs.FileSystem;
import gbench.common.tree.LittleTree.IRecord;
import gbench.commonApp.data.DataApp;

import static gbench.common.fs.FileSystem.*;
import static gbench.common.tree.LittleTree.*;
import static gbench.common.tree.LittleTree.IRecord.REC;

import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class JunitDicCn {
    
    @Test
    public void foo() {
        final var pattern = Pattern.compile("^\\s*(【([^】]+)】)(\\s*)(.+)$");
        final var ai = new AtomicInteger();
        final var words = new LinkedList<String>();
        lines(pathname2stream("现代汉语词典第五版.txt",this.getClass()), "utf16")
        .map(pattern::matcher)
        .filter(Matcher::matches)
        .forEach(matcher->{
            final var word = matcher.group(2);// 提取单词符号
            System.out.println(MFT("{0,number,#}\t{1}",ai.getAndIncrement(),word));
            words.add(word);
        });
        FileSystem.utf8write("C:\\Users\\gbench\\Desktop\\中国笔记\\逻辑词汇\\cn.txt", ()->{
            return words.stream().collect(Collectors.joining("\n"));
        });
    }
    
    @Test
    public void bar() {
        final var syms = "•’āáǎàōóǒòêēéěèīíǐìūúǔùǖǘǚǜü∥a-z"; //声调符号
        final var pattern = Pattern.compile("^\\s*(【([^】]+)】)(\\s*)(["+syms+"]+)(.*)$",Pattern.CASE_INSENSITIVE);
        System.out.println(pattern);
        
        final var line= "【做针线】zuòzhēn•xiɑn做缝纫刺绣等活计。";
        final var m = pattern.matcher(line);m.find();
        System.out.println("意义匹配：'"+m.group(5)+"'");
        //if(System.currentTimeMillis()>0)return ; // 这个正则式我写不对了
        
        // 定义一些有用的数据概念
        final var ai = new AtomicInteger();
        final var words = new LinkedList<String>();// 单词集合
        final class MyDataApp extends DataApp {
            public MyDataApp(String dbname) { this.reload(this.configureDbName(dbname));}
            public IRecord configureDbName(final String dbname) { return 
                cfg.derive("url", MFT("jdbc:mysql://localhost:3306/{0}?serverTimezone=GMT%2B8", dbname));
            }
        }; // MyDataApp 数据数据库操作类
        final var dataApp = new MyDataApp("db1"); // 指定数据库名
        final var tableschema = REC("#id",Integer.class,"word",String.class,"spell",String.class,"meaning",String.class);// 设计数据表
        final var tblname = "t_words";// 指定表名
        final var tbldefs = new SQL(tblname,tableschema).createTable(true);// 创建表定义sql语句
        
        // 数据操作
        dataApp.withTransaction(sess->{
            // 删除原有表格
            if(sess.sql2get(MFT("show tables like ''{0}''",tblname),boolean.class)) sess.sqlexecute(MFT("drop table {0}",tblname));
            
            //重新闯进表
            TUPLES(tbldefs).dropWhile(e->e._1()<2).map(e->e._2()).forEach(ln->trycatch((String sql)->sess.sqlexecute(sql)).apply(ln));
            
            // 数据插入操作
            lines(pathname2stream("现代汉语词典第五版.txt",this.getClass()), "utf16")
            .map(pattern::matcher)
            .filter(Matcher::matches)
            .forEach(matcher->{
                final var word = matcher.group(2);// 提取单词符号
                final var spell = matcher.group(4);// 提取单词拼音
                final var meaning = matcher.group(5);// 提取单词拼音
                final var rec = REC("word",word,"spell",spell,"meaning",meaning);// 注意不必生成id，因为已经采用了自增长类型 创建SQL语句了。
                System.out.println(MFT("{0,number,#}\t{1}\t{2}\t{3}",ai.getAndIncrement(),word,spell,meaning));
                words.add(word);
                final var insql = new SQL(tblname,rec).insert();
                trycatch(sess::sql2execute).apply(insql);
            }); // forEach
            
            // 文件书写
            FileSystem.utf8write("C:/Users/gbench/Desktop/中国笔记/逻辑词汇/cn.txt", ()->{ // 数据文件书写
                return words.stream().collect(Collectors.joining("\n"));
            }); // 把结果写入数据文件
        });// withTransaction
        
        System.out.println("\n程序运行结束！");
    }

}
